package blancosMikel.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;

import blancosMikel.controller.beans.noticiasBeans;

public class noticiasDAO extends SigaabmDAO {

	public ArrayList<noticiasBeans> selectNoticias() throws Exception {
		StringBuilder query = new StringBuilder();
		ArrayList<noticiasBeans> noticiasList = new ArrayList<noticiasBeans>();
		try {
			query.append("Select * from noticias");
			conn = this.getConnectionMYSQL();
			ps = conn.prepareStatement(query.toString());
			rs = ps.executeQuery();
			System.out
					.println("dao.noticias.selectNoticias.Ejecutado----------> "
							+ query.toString());

			noticiasBeans bean = null;
			while (rs.next()) {
				bean = new noticiasBeans();
				bean.setId_noticia(rs.getInt(1));
				bean.setNoticia(rs.getString(2));
				noticiasList.add(bean);
			}
			System.out
					.println("dao.noticias.selectNoticias----------> NoticiasCargado");

		} catch (SQLException error) {
			throw new Exception(error.getMessage());
		} catch (Exception error) {
			throw new Exception(error.getMessage());
		} finally {
			close(conn);
			close(ps);
			close(rs);
		}
		return noticiasList;
	}

	public int nextId() {
		int id = 0;
		try {
			StringBuilder query = new StringBuilder();
			query.append("Select id_noticia from noticias");
			conn = this.getConnectionMYSQL();
			ps = conn.prepareStatement(query.toString());
			rs = ps.executeQuery();
			System.out.println("dao.noticias.nextId.Ejecutado----------> "
					+ query.toString());

			while (rs.next()) {
				if (id < rs.getInt(1)) {
					id = rs.getInt(1);
				}
			}
			System.out.println("dao.noticias.nextId----------> Cargado");
		} catch (Exception e) {
			System.out.println("noticiasDao...Error..." + e.getMessage());
		} finally {
			close(conn);
			close(ps);
			close(rs);
		}
		return id + 1;
	}

	public void insertNoticia(noticiasBeans bean) throws Exception {
		try {
			StringBuilder query = new StringBuilder();
			conn = this.getConnectionMYSQL();

			query.append("INSERT INTO noticias ")
					.append("(id_noticia,noticia) ").append("values (")
					.append(bean.getId_noticia()).append(",'").append(bean.getNoticia())
					.append("')");

			st = conn.createStatement();
			st.executeUpdate(query.toString());
			System.out
					.println("dao.noticias.insertNoticias.Ejecutado----------> "
							+ query.toString());
			System.out
					.println("dao.noticias.insertNoticias----------> insertado");

		} catch (SQLException error) {
			throw new Exception(error.getMessage());
		} catch (Exception error) {
			throw new Exception(error.getMessage());
		} finally {
			close(conn);
			close(st);
		}
	}

	public void deleteNoticias(noticiasBeans bean) throws Exception {
		try {
			StringBuilder query = new StringBuilder();
			conn = this.getConnectionMYSQL();

			query.append("DELETE FROM noticias where id_noticia=").append(
					bean.getId_noticia());

			st = conn.createStatement();
			st.executeUpdate(query.toString());
			System.out
					.println("dao.noticias.deleteNoticia.Ejecutado----------> "
							+ query.toString());
			System.out
					.println("dao.noticias.deleteNoticia----------> eliminado");

		} catch (SQLException error) {
			throw new Exception(error.getMessage());
		} catch (Exception error) {
			throw new Exception(error.getMessage());
		} finally {
			close(conn);
			close(st);
		}
	}
}
